package com.share.PACManager;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

public class SelectDateEditText extends EditText {

	public SelectDateEditText(Context context) {
		super(context);
		initView(context);
	}
	
	public SelectDateEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	private void initView(Context context)
	{
		showArrow(true);
		setFocusable(false);
		setOnClickListener(onClickEditTime);
	}
	
	private OnClickListener onClickEditTime = new OnClickListener(){

		@Override
		public void onClick(final View v) {
			
			final EditText datetext = (EditText) v;
			
			DatePickerDialog.OnDateSetListener dateListener = 
				 new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						String text = String.format("%4d-%02d-%02d", year, monthOfYear+1, dayOfMonth);
						datetext.setText(text);
						datetext.setTag(new int[]{year, monthOfYear, dayOfMonth});
						v.setSelected(false);						
					}				  
			}; 

			int year, monthOfYear, dayOfMonth;
			
			int[] d = (int[]) datetext.getTag();
			if(d != null && d.length == 3)
			{
				year = d[0];
				monthOfYear = d[1];
				dayOfMonth = d[2];
				
				String text = String.format("%4d-%02d-%02d", year, monthOfYear+1, dayOfMonth);
				datetext.setText(text);
			}
			else
			{
				Calendar calendar = Calendar.getInstance();
				
				year = calendar.get(Calendar.YEAR);
				monthOfYear = calendar.get(Calendar.MONTH);
				dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			}
			
			
			DatePickerDialog dialog = new DatePickerDialog(v.getContext(),dateListener,
					year, monthOfYear, dayOfMonth);
			dialog.setOnCancelListener(new OnCancelListener(){
				@Override
				public void onCancel(DialogInterface dialog) {
					v.setSelected(false);
				}				
			});
			
			
			dialog.show();
			v.setSelected(true);			
		}		
	};
	
	private boolean showArrow;	
	public void showArrow(boolean show)
	{
		showArrow = show;
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		if(!showArrow)
		{
			super.onDraw(canvas);
			return;
		}
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.GRAY);
		Path path = new Path();
		
		int x = getWidth() - 24;
		int y = getHeight() / 2;
		int r = 10;		
		
		if(isSelected())
		{
			path.moveTo(x - r, y+r/2);
			path.lineTo(x + r, y+r/2);
			path.lineTo(x, y-r);
		}
		else
		{
			path.moveTo(x, y+r);
			path.lineTo(x - r, y-r/2);
			path.lineTo(x + r, y-r/2);			
		}
		
		path.close();		
		canvas.drawPath(path, paint);		
		super.onDraw(canvas);
	}
}
